<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/7 0007
 * Time: 9:24
 */

namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use App\Models\ProductType;
use App\Models\Product;
use App\Models\Exchange;
use Illuminate\Support\Facades\Input;
use Validator;

class BonusController extends Controller
{
    //product_type表增删改查的开始
    //product_type表的添加功能
    public function anyType(Request $request,$id=null){
//        $data=DB::table('product_type')->get();
       $data = DB::table('product_type')->orderBy('created_at','desc');
        if($request->has('keyword') && $request->input('keyword')){
            $data->where('name', 'like', '%'.$request->input('keyword').'%')
                ->orWhere('description', 'like', '%'.$request->input('keyword').'%');
        }
        $data =$data ->paginate(20);
//        print_r($data);
//        exit();
        return view('admin.bonus.index', [
            'data'=>$data,
            'appends' => $id,
    ]);

    }

    public function getUpdate($id=0){

        $data = ProductType::findOrNew($id);

        return view('admin.bonus.edit', ['data' => $data]);
    }

    public function postUpdate(Request $request, $id=0 ){

        $model =  ProductType::findOrNew($id);
        $data = $request->except('_token');
//        unset($data['_token']);
        $attributes = [
            'name'=>'产品名称',
            'description'=>'产品描述',
        ];
        $validator=Validator::make( $data,
            ['name' => 'required',
            ],
            [
                'required' => ":attribute不能为空",
//                'name' => '产品名称不能为空',
            ],
            $attributes);
        if($validator->passes()){
//            $re= ProductType::create($data);
            $model->fill($data);
            $model->save();//写入数据库
            if ($model->id) {
                $ret = ['state' => 'success', 'message' => '操作成功'];
            } else {
               $ret['message'] = '保存出错';

            }
        }else{
            $message = $validator->messages();
            $ret['message'] = $message;
            foreach ($attributes as $k => $attr) {
                if ($message->has($k)) {
                    $ret['state'] = 'fail';
                    $ret['message'] = $message->first($k);
                    break;
                }
            }
        }
         return \Response::json($ret);
    }
    //product_type表的删除功能
    public function postDelete($id)
    {
        $ret = ['state' => 'fail', 'message' => '未知错误，请刷新重试'];
        if (ProductType::destroy($id)) {
            $ret = ['state' => 'success', 'message' => '删除成功'];
        }
        return \Response::json($ret);
    }

    //product_type表增删改查的结束


    //product表增删改查的开始
    //product表的添加功能
    public function anyFinancial(Request $request,$id=null)
    {
//        $data=Product::get();
        $data = Product::
            leftJoin('product_type','product.productTypeId','=','product_type.id')->orderBy('created_at','desc')
            ->select('product.*','product_type.name');
        if($request->has('search') && $request->input('search')){
            $data->where('name', 'like', '%'.$request->input('search').'%')
                ->orWhere('title', 'like', '%'.$request->input('search').'%');
        }
        $data =$data ->paginate(20);

//        print_r($data);
//        exit();
        return view('admin.product.index', ['data'=>$data,'appends' => $id]);
    }

    public function getUpd($id=0){

        $model = Product::findOrNew($id);
        $model['pics'] = explode(',', $model['pics']);
        $productType = ProductType::all();

//        $productType =  Product::all();
//        print_r($product);
//        exit();
        //print_r($productType); exit();
        return view('admin.product.compile', [
            'model' => $model,
            'types' => $productType,
        ]);
    }
    public function postUpd(Request $request, $id=0 ){

        $model =  Product::findOrNew($id);
        $data = $request->except('_token');
//        unset($data['_token']);
        $attributes = [
            'spec'=>'商品规格',
            'productTypeId'=>'商品类型ID',
            'title'=>'商品标题',
            'bonus'=>'兑换商品需要的积分',
            'detail'=>'商品详情',
        ];
        $validator=Validator::make( $data,
            [
                'spec'=>'required',
                'productTypeId'=>'required',
                'title'=>'required',
                'bonus'=>'required',
                'detail'=>'required',
            ],
            [
                'required' => ":attribute不能为空",

            ],
            $attributes);
        if($validator->passes()){
//            $re= ProductType::create($data);
            $data['pics'] = implode(',', $data['pics']);
            $model->fill($data);
            $model->save();//写入数据库
            if ($model->id) {
                $ret = ['state' => 'success', 'message' => '操作成功'];
            } else {
                $ret['message'] = '保存出错';

            }
        }else{
            $message = $validator->messages();
            $ret['message'] = $message;
            foreach ($attributes as $k => $attr) {
                if ($message->has($k)) {
                    $ret['state'] = 'fail';
                    $ret['message'] = $message->first($k);
                    break;
                }
            }
        }
        return \Response::json($ret);
    }

    //product表的删除功能
    public function postDel($id)
    {
        $ret = ['state' => 'fail', 'message' => '未知错误，请刷新重试'];
        if (Product::destroy($id)) {
            $ret = ['state' => 'success', 'message' => '删除成功'];
        }
        return \Response::json($ret);
    }
    //product表增删改查的结束


    //exchange表增删改查的开始
    //exchange表的添加功能
    public function anyOrder(Request $request,$id=null)
    {
        $data = Exchange::
            leftJoin('product','exchange.productId','=','product.id')
            ->leftJoin('users','exchange.userId','=','users.id')
            ->leftJoin('user_address','exchange.addressId','=','user_address.id')
            ->orderBy('created_at','desc')
            ->select('exchange.*','product.title','users.nickName','user_address.address');
        if($request->has('search') && $request->input('search')){
            $data->where('title', 'like', '%'.$request->input('search').'%')
                ->orWhere('nickName', 'like', '%'.$request->input('search').'%');
        }
        $data =$data ->paginate(20);
        return view('admin.exchange.index', ['data'=>$data,'appends' => $id]);
    }
    //exchange表的删除功能
    public function postDele($id)
    {
        $ret = ['state' => 'fail', 'message' => '未知错误，请刷新重试'];
        if (Exchange::destroy($id)) {
            $ret = ['state' => 'success', 'message' => '删除成功'];
        }
        return \Response::json($ret);
    }


    //exchange表增删改查的结束

}